import React, { useState, useMemo, useEffect } from 'react';
import { d_sort, cline, no_del, useHigh, GridTable, filter_row, useCmd, BtTxt, WaitUmd, MainView,
    GateWay, PanelFind, Btn, is_empty, loc } from 'sui';
import { Hand } from './hand';
import { useAccess } from 'cache';
import { fix_col, reload, importCfg, c_load } from './cmd';

const SpcCfg = () => {
    const [main, setMain] = useState();
    const [para, setPara] = useState();
    const [no_access] = useAccess();
    const [high] = useHigh(140, 100);
    const umd = useCmd(null, "spc");

    useEffect(() => {
        /*
        urlGet(null, c_load,
            r => {
                if (r.cell) {
                    d_sort(r.cell, "sn");
                }

                if (r.station) {
                    d_sort(r.station, "sn");
                }

                setPara(r);
                reload(r, setMain);
            }, e => setPara(null));
            */
    }, []);

    const filterRow = useMemo(() => {
        if (is_empty(main)) {
            return null;
        }

        const flst = ["sn", "name", "ltp"];

        return filter_row(main, flst, umd.filter);
    }, [main, umd.filter]);

    const hret = (v) => {
        umd.hreturn(false);
        if (v) {
            reload(para, setMain)
        }
    };

    if (umd.fin === "edit") {
        return <Hand cur={umd.cur} hreturn={hret} para={para} main={main}/>;
    }

    if (umd.fin === "add") {
        return <Hand hreturn={hret} para={para} main={main}/>;
    }

    const hdel = (uid) => {
        if (no_del()) {
            return;
        }

        const cmd = {r: [{
            p: cline(),
            n: "spc",
            u: uid,
        }]};

        //urlM(null, cmd, r => reload(para, setMain, umd.endWait), e => umd.setWront(loc("mg_039")));
    };

    const fix = fix_col();

    const col = [
        {name: loc("l_193"), width: 60, key: 'index'},
        {name: loc("l_238"), key: 'e_opt', width: 120, export: false, formatter: props => {
            const d = props.row;

            return (
                <div className="inline-flex">
                    <BtTxt umd={umd} hcmd={() => hdel(d.uid)} disabled={no_access("aa")}>{loc("l_237")}</BtTxt>
                    <BtTxt hcmd={() => umd.hcur(d, "edit")} disabled={no_access("ac")}>{loc("l_235")}</BtTxt>
                </div>
            );
        }},
        ...fix,
    ];

    const htemp = () => {
        if (is_empty(col)) {
            alert(loc("db_018"));
            return;
        }

        umd.startWait();

        const tmp = [];
        col.forEach(d => {
            if (d.key === "index" || d.key === "e_opt") {
                return;
            }

            tmp.push({value: d.name});
        })

        //save_xlsx([tmp], "template_col.xlsx", umd);
    };

    const hcsv = () => {
        if (is_empty(col) || is_empty(main)) {
            alert(loc("db_018"));
            return;
        }

        umd.startWait();

        const tmp = [];
        col.forEach(d => {
            if (d.key === "index" || d.key === "e_opt") {
                return;
            }

            let key = d.key;

            if (key === "ltp") {
                key = "tp";
            }
            else if (key === "ldsn") {
                key = "dsn";
            }
            else if (key === "lfsn") {
                key = "fsn";
            }
            else if (key === "lwsn") {
                key = "wsn";
            }

            tmp.push({value: d.name, k: key});
        })

        const row = [tmp];

        main.forEach(d => {
            const tp = [];
            tmp.forEach(t => tp.push({value: d[t.k]}));

            row.push(tp);
        });

        //save_xlsx(row, "spc_cfg.xlsx", umd);
    };

    const Right = () => {
        const action=`${GateWay.getUpload()}/${cline()}/tmpspc`;

        const hup = (fnDone, fn) => {
            /*
            const p = `/upload/${cline()}/tmpspc/${fn}`;

            urlFileChg("array", {p: p},
                r => importCfg(fnDone, r, col, main, para, umd, setMain),
                e => fnDone());
                */
        };

        return (
            <>
                <Btn className="mr-4" hcmd={() => umd.hcur(null, "add")} disabled={no_access("ag")}>{loc("zw_149")}</Btn>
                <Btn className="mx-4" hcmd={hcsv}>{loc("nui_03")}</Btn>
                <Btn hcmd={htemp}>{loc("z_07")}</Btn>
            </>
        );
    };

    return (
        <MainView>
            <PanelFind left={loc("zw_148")} right={<Right/>} hcmd={v => umd.setFilter(v)}/>
            <GridTable col={col} row={filterRow} high={high}/>
            <WaitUmd umd={umd}/>
        </MainView>
    );
};

export {SpcCfg};
